#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

struct edge {
	int u, w;
	edge(int ui, int wi) {
		u = ui;
		w = wi;
	}
};

int n, p;
vector<edge> GS[105];
int U[105];
int C[105];
bool flag[105];
bool nullflag;

void push_down(int x) {
	if (C[x] > 0) return;
	for (unsigned i = 0; i < GS[x].size(); ++i) {
		push_down(GS[x][i].u);
		C[x] += max(C[GS[x][i].u], 0) * GS[x][i].w;
	}
	C[x] -= U[x];
}

int main() {
	int x, y, w;
	cin >> n >> p;
	for (int i = 1; i <= n; ++i) {
		cin >> C[i] >> U[i];
	}

	for (int i = 1; i <= p; ++i) {
		cin >> x >> y >> w;
		GS[y].push_back(edge(x, w));
		flag[x] = 1;
	}
	
	for (int i = 1; i <= n; ++i) {
		if (!flag[i]) push_down(i);
	}

	nullflag = 1;
	for (int i = 1; i <= n; ++i) {
		if (!flag[i] && C[i] > 0) {
			nullflag = 0;
			break;
		}
	}
	if (nullflag) {
		cout << "NULL" << endl;
	} else {
		for (int i = 1; i <= n; ++i) {
			if (C[i] > 0 && !flag[i]) cout << i << " " << C[i] << endl;
		}
	}
	return 0;
}